package com.ssm;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;

public class DbManager {

	private IdeaDB iDB;
	private KeywordDB kDB1; //keywords box database
	private KeywordDB kDB2; //keywords box database
	private KeywordDB kDB3; //keywords box database
	private SQLiteDatabase db;
	Context context;
	
	public DbManager(Context c){
		context =  c;
		//Initialize Database
		 iDB = new IdeaDB(context,"ideabox");
	     kDB1 = new KeywordDB(context,"keyword1");
	     kDB2 = new KeywordDB(context,"keyword2");
	     kDB3 = new KeywordDB(context,"keyword3");
		
		
	}
	
	public void addKeyword1(String text){
		try{
			db = kDB1.getWritableDatabase();
			ContentValues row = new ContentValues();
			row.put("keyword", text);
			db.insert("keyword1", null, row);
		}catch(SQLiteException e){
			e.printStackTrace();
		}
		
		
	}
	public void addKeyword2(String text){
		try{
			db = kDB2.getWritableDatabase();
			ContentValues row = new ContentValues();
			row.put("keyword", text);
			db.insert("keyword2", null, row);
		}catch(SQLiteException e){
			e.printStackTrace();
		}
		
		
	}
	public void addKeyword3(String text){
		try{
			db = kDB3.getWritableDatabase();
			ContentValues row = new ContentValues();
			row.put("keyword", text);
			db.insert("keyword3", null, row);
		}catch(SQLiteException e){
			e.printStackTrace();
		}
		
		
	}
	
	public String getKeyword1(int number){
		
		try{
			db = kDB1.getReadableDatabase();
			Cursor cursor = db.rawQuery("SELECT * FROM keyword1 WHERE _id="+number+"",null);
			
			cursor.moveToFirst();
			return cursor.getString(1);
		}catch(SQLiteException e){
			e.printStackTrace();
		}
		return null;
		
	}
	
	public String getKeyword2(int number){
		try{
		db = kDB2.getReadableDatabase();
		
		Cursor cursor = db.rawQuery("SELECT * FROM keyword2 WHERE _id="+number+"",null);
		cursor.moveToFirst();
		return cursor.getString(1);
		}catch(SQLiteException e){
			e.printStackTrace();
		}
		return null;
	}

	public String getKeyword3(int number){
	
		try{
		db = kDB3.getReadableDatabase();
		
		Cursor cursor = db.rawQuery("SELECT * FROM keyword3 WHERE _id="+number+"",null);
		cursor.moveToFirst();
		return cursor.getString(1);
		}catch(SQLiteException e){
			e.printStackTrace();
		}
		return null;
	}
	
	public void addList(String key1,String key2, String key3, String detail){
		try{
			db = iDB.getWritableDatabase();
			ContentValues row = new ContentValues();
			row.put("key1", key1);
			row.put("key2", key2);
			row.put("key3", key3);
			row.put("detail", detail);
			
			db.insert("ideabox", null, row);
		}catch(SQLiteException e){
			e.printStackTrace();
		}
		iDB.close();
		
	}
	public void CloseDB(){
		db.close();
	}
	
}
